package com.tianyao.compose.baseuse.activity.animate

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.viewModels
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.core.MutableTransitionState
import androidx.compose.animation.expandHorizontally
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.animation.shrinkHorizontally
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.Button
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.tianyao.compose.baseuse.common.logd
import com.tianyao.compose.baseuse.viewmodel.AnimateViewModel
import com.tianyao.compose.baseuse.viewmodel.StableKeyList
import kotlin.concurrent.timer

/**
 * 动画的Activity
 */
class AnimatedActivity : ComponentActivity() {

    private val animateViewModel by viewModels<AnimateViewModel>()

    private var data = StableKeyList<String>().apply {
        add("Android")
        add("kotlin")
        add("Java")
    }

    @OptIn(ExperimentalMaterial3Api::class)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContent {
            Column {
                MyList()
                TestButton()
            }
        }
    }

    @Composable
    fun MyList() {
        LazyColumn {
            items(data.getItemsForCompose(), key = { it.key }) { wrapper ->
                Text(wrapper.value)
            }
        }
    }

    @Preview
    @Composable
    fun TestButton() {
        Column {
            Button(
                onClick = {
                    data.add("Compose")
                    val itemWrapper = data.getItem(1)
                    itemWrapper?.let {
                        data.updateByValue(it.key, "Hello World")
                    }

                },
            ) {
                Text("修改list的值")
            }
        }
    }


    @Composable
    fun TestMutableTransitionState() {
        var isStopLog = false
        val state = remember { MutableTransitionState(false).apply { targetState = true } }

        Column(
            modifier = Modifier
                .fillMaxSize()
                .padding(16.dp),
            verticalArrangement = Arrangement.spacedBy(16.dp)
        ) {
            // 第一个按钮，显示动画
            Button(onClick = {
                state.targetState = true
                isStopLog = false
            }) {
                Text("Show Animation", fontSize = 22.sp)
            }

            // 第二个按钮，隐藏动画
            Button(onClick = {
                state.targetState = false
                isStopLog = false
            }) {
                Text("Hide Animation", fontSize = 22.sp)

            }

            //MutableTransitionState传入AnimatedVisibility
            AnimatedVisibility(
                visibleState = state,
                enter = fadeIn() + expandHorizontally(),
                exit = fadeOut() + shrinkHorizontally()
            ) {
                Text(
                    text = "Hello Compose", fontSize = 36.sp, color = Color.Black
                )
            }
        }

        //定时获取动画的状态
        LaunchedEffect(Unit) {
            timer(period = 100) {
                if (!isStopLog) {
                    when (val animationState = state.getAnimationState()) {
                        AnimState.INVISIBLE -> {
                            "动画的状态:${animationState.name}".logd()
                            isStopLog = true
                        }

                        AnimState.VISIBLE -> {
                            "动画的状态:${animationState.name}".logd()
                            isStopLog = true
                        }

                        else -> {
                            "动画的状态:${animationState.name}".logd()
                        }
                    }
                }
            }
        }
    }

    /**
     * 动画的各种状态
     */
    private fun MutableTransitionState<Boolean>.getAnimationState(): AnimState {
        return when {
            this.isIdle && this.currentState -> AnimState.VISIBLE   //动画已经结束,当前处于可见状态
            this.isIdle && !this.currentState -> AnimState.INVISIBLE   //动画已经结束,当前处于不可见状态
            !this.isIdle && this.currentState -> AnimState.DISAPPEARING   //动画执行中,且逐渐不可见
            else -> AnimState.APPEARING          //动画执行中,且逐渐可见
        }
    }

    /**
     * 动画状态的枚举
     */
    enum class AnimState {
        VISIBLE, INVISIBLE, APPEARING, DISAPPEARING
    }

}

